package flc.ast.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.List;

import flc.ast.R;
import flc.ast.activity.AlbumActivity;
import flc.ast.bean.MyAlbumBean;
import stark.common.basic.view.container.StkLinearLayout;

public class AlbumAdapter1 extends RecyclerView.Adapter {

    private Context mContext;
    private List<MyAlbumBean> list;
    private int selPosition;

    public AlbumAdapter1(Context mContext, List<MyAlbumBean> list) {
        this.mContext = mContext;
        this.list = list;
    }

    public int getSelPosition() {
        return selPosition;
    }

    public void setSelPosition(int selPosition) {
        this.selPosition = selPosition;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_album, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        MyViewHolder myViewHolder = (MyViewHolder) holder;
        if (list.get(position).getListData().size()==0||list.get(position).getListData()==null){
            myViewHolder.imageView.setImageResource(R.drawable.wuzhaopianxiangce1);
        }else{
            Glide.with(mContext).load(list.get(position).getListData().get(0)).into(myViewHolder.imageView);
        }
        myViewHolder.tvAlbumItemName.setText(list.get(position).getName());
        myViewHolder.tvAlbumItemNum.setText(list.get(position).getListData().size() + mContext.getString(R.string.unit_album));
        if (selPosition==position){
            myViewHolder.tvAlbumItemNum.setVisibility(View.VISIBLE);
            myViewHolder.tvAlbumItemName.setVisibility(View.VISIBLE);
            myViewHolder.llAlbumItem.setVisibility(View.INVISIBLE);
        }else{
            myViewHolder.tvAlbumItemNum.setVisibility(View.INVISIBLE);
            myViewHolder.tvAlbumItemName.setVisibility(View.INVISIBLE);
            myViewHolder.llAlbumItem.setVisibility(View.VISIBLE);
        }
        holder.itemView.setTag(list.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (position==selPosition){
                    AlbumActivity.albumId=list.get(position).getId();
                    AlbumActivity.isPub=list.get(position).isPublic();
                    mContext.startActivity(new Intent(mContext,AlbumActivity.class));
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView tvAlbumItemName,tvAlbumItemNum;
        private StkLinearLayout llAlbumItem;
        private ImageView imageView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tvAlbumItemName = itemView.findViewById(R.id.tvAlbumItemName);
            tvAlbumItemNum= itemView.findViewById(R.id.tvAlbumItemNum);
            imageView=itemView.findViewById(R.id.ivAlbumItemImg);
            llAlbumItem=itemView.findViewById(R.id.llAlbumItem);
        }
    }

}
